﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace SharingCenter.Web.Classes.Messages
{
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes;
    using System.Xml.Linq;
    using ClassLibrary;

    public class MessageOperations
    {
        static XElement MakeRecipientList(Guid to)
        {
            XElement recipient = new XElement("RecipientList");
            List<Guid> toList = new List<Guid>();
            toList.Add(to);
            foreach (Guid u in toList)
            {
                recipient.Add(new XElement("Recipient",
                    new XAttribute("UserId", u),
                    new XAttribute("Read", false)));
            }
            return recipient;
        }

        public static void SendSystemMessage(string appName, string eventName, string xsltFolderName, Guid sender, Guid to, XElement context, bool submit)
        {
            Sc_SiteMessage msg = new Sc_SiteMessage();
            msg.AppName = appName;
            msg.SenderUserId = sender;
            msg.EventName = eventName;
            msg.MessageDate = DateTime.Now;
            msg.RecipientList = MakeRecipientList(to);
            msg.ContextXml = new XElement("Context",
                new XAttribute("XsltFolderInMod", xsltFolderName),
                new XElement("Event",
                    new XAttribute("Name", eventName),
                    context.Elements()));
            scDataContext.Instance.Sc_SiteMessages.InsertOnSubmit(msg);
            if (submit)
            {
                scDataContext.Instance.SubmitChanges();
            }
        }

        public static void SendSystemMessage(Guid sender, Guid to, string siteEvent, XElement context, bool submit)
        {
            string cryptedContext = StringHelper.GetMd5Sum(context.ToString());
            using (scDataContext dc = new scDataContext())
            {
                if (!IsMessageDuplicatedInSameDay(sender, to, context))
                {
                    Sc_SiteMessage msg = new Sc_SiteMessage();
                    msg.AppName = EntityType.Default.ToString();
                    msg.SenderUserId = sender;
                    msg.EventName = siteEvent;
                    msg.MessageDate = DateTime.Now;
                    msg.RecipientList = MakeRecipientList(to);
                    msg.ContextXml = context;
                    msg.ContextCrypt = cryptedContext;
                    dc.Sc_SiteMessages.InsertOnSubmit(msg);
                }
                else
                {
                    dc.UpdateMessageDateBaseOnContextCrypt(cryptedContext);
                    // resotres read flag to false
                }

                dc.SubmitChanges();
            }
        }

        public static bool IsMessageDuplicatedInSameDay(Guid sender, Guid to, XElement context)
        {
            return Convert.ToBoolean(scDataContext.Instance.IsMessageDuplicatedInSameDay(StringHelper.GetMd5Sum(context.ToString())));
        }

        //public static void SendSystemMessage(Guid sender, Guid to, string message, EntityType contextType, Guid contextId, RequestStatus status, bool submit)
        //{
        //    Sc_Message msg = new Sc_Message();
        //    msg.MessageId = Guid.NewGuid();
        //    msg.SenderEntityType = EntityType.Default;
        //    msg.SenderId = sender;
        //    msg.IsMain = true;
        //    msg.ContextType = contextType;
        //    msg.ContextId = contextId;
        //    msg.MessageContent = message;
        //    msg.Status = status;
        //    msg.MessageDate = DateTime.Now;

        //    List<object> guidList = new List<object>();
        //    guidList.Add(to);
        //    msg.RecipientList = Utility.SerializeSimpleList(guidList);
        //    scDataContext.Instance.Sc_Messages.InsertOnSubmit(msg);
        //    if (submit)
        //    {
        //        scDataContext.Instance.SubmitChanges();
        //    }
        //}

        //public static void SendMessage(EntityType senderEntity, Guid from, Guid to, string message, EntityType contextType, Guid? contextId1, Guid? contextId2, bool submitChanges)
        //{
        //    if (senderEntity == EntityType.Default)
        //    {
        //        from = WebConsts.FAKE_GUID;
        //    }

        //    Sc_Message msg = new Sc_Message();
        //    msg.MessageId = Guid.NewGuid();
        //    msg.IsMain = true;
        //    msg.ContextType = contextType;
        //    msg.ContextId = contextId1;
        //    msg.ContextId2 = contextId2;
        //    msg.MessageContent = message;
        //    msg.MessageDate = DateTime.Now;

        //    List<object> guidList = new List<object>();
        //    guidList.Add(to);
        //    msg.RecipientList = Utility.SerializeSimpleList(guidList);

        //    scDataContext.Instance.Sc_Messages.InsertOnSubmit(msg);

        //    if (submitChanges)
        //    {
        //        scDataContext.Instance.SubmitChanges();
        //    }
        //}

        public static void MarkMessageRead(Guid userId, DateTime lastRetrievalDate)
        {
            return;
            using (scDataContext cxt = new scDataContext())
            {
                cxt.MarkMessageAsRead(userId, lastRetrievalDate);
            }
        }

        //public static List<Sc_Message> GetMessages(Guid toUserId)
        //{
        //    var q = from t in scDataContext.Instance.Sc_Messages
        //            where t.RecipientList.Contains(toUserId.ToString())
        //            select t;

        //}
    }
}
